#!/bin/bash

#consts
pwd_dir="`pwd`/"
ssh_src_account='marcin@localhost'
ssh_src_port=2023
ssh_src="ssh -p $ssh_src_port $ssh_src_account"
dir_repo_src='/home/marcin/winhome/workspace/teamster_trunk/'
ssh_dest_account=''
ssh_dest=''
dir_repo_dest='/home/marcin/winhome/workspace/teamster_trunk/'
status_check=16
sync_files=4
changed_files_file_name="${pwd_dir}changed_files.txt"

#params
param_u=false
param_s=false
param_lu=false
param_lu_sec=$status_check
param_ls=false
param_ls_sec=$sync_files

last_param=''
for p in $@
do
	if [ "$p" = "-u" ]; then
		param_u=true;
	elif [ "$p" = "-s" ]; then
		param_s=true;
	elif [ "$p" = "-lu" ]; then
		param_lu=true
	elif [ "$last_paam" = "-lu" ]; then
		param_lu_sec=$p
	elif [ "$p" = "-ls" ]; then
		param_ls=true
	elif [ "$last_param" = "-ls" ]; then
		param_ls_sec=$p
	fi
	last_param=$p
done

dir_src="$dir_repo_src"
dir_dest="$dir_repo_dest"
changed_files=""
x=0

while [ true ]
do
	if [ $param_u = true -o $param_lu = true -a `expr $x % $param_lu_sec` = 0 ]; then
		if [ "$ssh_src" != "" ]; then
			revision_src="`$ssh_src \"cd $dir_src && svn info | grep -E 'Revision|Wersja' | sed 's/Revision: //g' | sed 's/Wersja: //g'\"`"
		else
			revision_src="`cd $dir_src && svn info | grep Revision | sed 's/Revision: //g'`"
		fi
		if [ "$ssh_dest" != "" ]; then
			revision_dest="`$ssh_dest \"cd $dir_repo_dest && svn info | grep -E 'Revision|Wersja' | sed 's/Revision: //g' | sed 's/Wersja: //g'\"`"
		else
			revision_dest="`cd $dir_repo_dest && svn info | grep Revision | sed 's/Revision: //g'`"
		fi
		echo "status_check: src revison: $revision_src, dest revision: $revision_dest"
		if [ $revision_src != $revision_dest ]; then
			echo "updating svn destination"
			cd "$dir_repo_dest" && svn update --force -r $revision_src && cd $pwd_dir
			echo "svn destination has bean updated"
		fi
		echo "`$ssh_src "cd $dir_src && svn status | sed 's/\\\\\\/\\//g' | sed 's/.\ *//' | sed 's/\\ /\\\*/g'"`" > "$changed_files_file_name"
		echo "status_check complete"
	fi
	if [ $param_s = true -o $param_ls = true -a `expr $x % $param_ls_sec` = 0 ]; then
		changed_files_count=`cat "$changed_files_file_name" | wc -l`;
		if [ $changed_files_count -ge 0 ]; then
			echo "synchronizing $changed_files_count files in dirs: \"$dir_src\" and \"$dir_dest\""
			for f in `cat "$changed_files_file_name"`
			do
        			file_name="`echo \"$f\" | sed 's/\*/\\\\ /g'`"
				echo "file: \"$file_name\""
				#nohup rsync -vue "ssh -p $ssh_src_port" "$ssh_src_account:$dir_src$file_name" "$dir_dest$file_name" 1>>nohup.out 2>>nohup_err.out &
				nohup scp -P $ssh_src_port "$ssh_src_account:$dir_src$file_name" "$dir_dest$file_name" 1>>nohup.out 2>>nohup_err.out &
			done
			echo 'Waiting for rsync files'
			while [ "`jobs | grep Running`" != "" ]; do sleep 1; done
			echo "synchronizing files finished"
		else
			echo "nothing to synchronizing"
		fi
	
	fi
	
	if [ $param_lu = false -a $param_ls = false ]; then
		exit;
	fi
	
	sleep 1
	x=`expr $x + 1`

done

echo "script finished"

